/*
 * @(#)FileExtract.java $version 2012. 4. 5.
 *
 * Copyright 2007 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.file.extract;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hs
 */
public class FileExtract {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws Exception {
		FileReader fr = new FileReader("D:\\nts\\work\\ecms\\ecms_doing\\lucy_db.log.2012-04-02_2");
		FileWriter fw = new FileWriter("D:\\nts\\work\\ecms\\ecms_doing\\lucy_db.log.2012-04-02_INSERT.result");
		BufferedWriter bw = new BufferedWriter(fw);
		PrintWriter outFile = new PrintWriter(bw);

		BufferedReader inFile = new BufferedReader(fr);
		String readLine = null;
		StringBuilder bufferPrint = new StringBuilder();
		String systimestamp = null;
		String stdStrToFindContId = "FROM ec_inqr_hist WHERE inqr_id = ";
		final String INSERT = "com.nhncorp.echo.counsel.inquiry.inquiryHistoryModel.insertInquiryHistory";
		final String UPDATE = "com.nhncorp.echo.counsel.inquiry.inquiryHistoryModel.updateTretYn";

		boolean toPrint = false;

		// INSERT 추출
		if (false) {
			while ((readLine = inFile.readLine()) != null) {
				if (readLine.contains(INSERT)) {
					if (toPrint) {
						writingToFileForINSERT(outFile, bufferPrint, stdStrToFindContId);

					}

					//날짜추출
					systimestamp = "to_timestamp('" + readLine.substring(0, readLine.indexOf("[")).trim() + "', 'YYYY-MM-DD HH24:MI:SS')";
					toPrint = true;
				} else if (readLine.contains("executing ibatis statement")) {
					writingToFileForINSERT(outFile, bufferPrint, stdStrToFindContId);

					toPrint = false;
				}

				if (toPrint) {
					if (!readLine.contains("executing ibatis statement")) {
						if (readLine.contains("systimestamp")) {
							readLine = readLine.replace("systimestamp", systimestamp);
						}

						bufferPrint.append(" ").append(readLine);
					}
				}

			}

			writingToFileForINSERT(outFile, bufferPrint, stdStrToFindContId);
			outFile.close();
		}

		//UPDATE 추출
		if (true) {
			while ((readLine = inFile.readLine()) != null) {
				if (readLine.contains(UPDATE)) {
					if (toPrint) {
						writeToFileForUPDATE(outFile, bufferPrint);
					}
					toPrint = true;
				} else if (readLine.contains("executing ibatis statement")) {
					writeToFileForUPDATE(outFile, bufferPrint);
					toPrint = false;
				}

				if (toPrint) {
					if (!readLine.contains("executing ibatis statement")) {
						bufferPrint.append(" ").append(readLine);
					}
				}

			}

			outFile.close();

		}

		System.out.println("COMPLETED.");

	}

	/**
	 * @param outFile
	 * @param bufferPrint
	 */
	private static void writeToFileForUPDATE(PrintWriter outFile, StringBuilder bufferPrint) {
		if (bufferPrint.length() > 2) {
//			if (isValidQuery(bufferPrint.toString())) {
//				System.out.println(bufferPrint.toString() + ";");
//				outFile.println(bufferPrint.toString() + ";");
//			}
			
			System.out.println(bufferPrint.toString() + ";");
		}
		bufferPrint.delete(0, bufferPrint.length());
	}

	/**
	 * @param outFile
	 * @param bufferPrint
	 * @param stdStrToFindContId
	 */
	private static void writingToFileForINSERT(PrintWriter outFile, StringBuilder bufferPrint, String stdStrToFindContId) {
		String inqId;
		String ansId;
		if (bufferPrint.toString().contains("ans_id")) {
			String tmpStr = bufferPrint.toString().replace("VALUES      (", "SELECT ");

			//			문의ID추출
			inqId = tmpStr.substring(tmpStr.indexOf(stdStrToFindContId) + stdStrToFindContId.length(), tmpStr.indexOf(stdStrToFindContId) + stdStrToFindContId.length() + 19);

			//			답변ID추출
			ansId = tmpStr.substring(tmpStr.indexOf("            ,( select to_timestamp('") - 19, tmpStr.indexOf("            ,( select to_timestamp('"));

			tmpStr = tmpStr.substring(0, tmpStr.lastIndexOf(")"));
			tmpStr = tmpStr + "FROM db_root WHERE NOT EXISTS (SELECT 1 FROM ec_inqr_hist WHERE inqr_id = " + inqId + " AND ans_id = " + ansId + ");";

			List<String> targetList = new ArrayList<String>();
			targetList.add("2201204021003523331");
			targetList.add("2201204011003522626");
			targetList.add("2201204021003524635");
			targetList.add("2201203311003519340");
			targetList.add("2201204021003524619");
			targetList.add("2201203291003509687");
			targetList.add("2201204020003523384");
			targetList.add("2201204021003525157");
			targetList.add("2201204020003525319");
			targetList.add("2201204020003525413");
			targetList.add("2201204021003524358");
			targetList.add("2201204021003525133");
			targetList.add("2201204020003526120");
			targetList.add("2201204021003526427");
			targetList.add("2201203301003516072");
			targetList.add("2201203311003518987");
			targetList.add("2201204021003524909");
			targetList.add("2201204021003525482");
			targetList.add("2201204021003526020");

			if (targetList.contains(inqId)) {
				System.out.println(bufferPrint.toString() + ";");
				outFile.println(bufferPrint.toString() + ";");
			}
		}

		bufferPrint.delete(0, bufferPrint.length());
	}

	private static boolean isValidQuery(String src) {
		List<String> targetList = new ArrayList<String>();
		targetList.add("2201204021003523331");
		targetList.add("2201204011003522626");
		targetList.add("2201204021003524635");
		targetList.add("2201203311003519340");
		targetList.add("2201204021003524619");
		targetList.add("2201203291003509687");
		targetList.add("2201204020003523384");
		targetList.add("2201204021003525157");
		targetList.add("2201204020003525319");
		targetList.add("2201204020003525413");
		targetList.add("2201204021003524358");
		targetList.add("2201204021003525133");
		targetList.add("2201204020003526120");
		targetList.add("2201204021003526427");
		targetList.add("2201203301003516072");
		targetList.add("2201203311003518987");
		targetList.add("2201204021003524909");
		targetList.add("2201204021003525482");
		targetList.add("2201204021003526020");

		for (String target : targetList) {
			if (src.indexOf(target) != -1) {
				return true;

			}
		}

		return false;

	}
}
